package com.ngkj.zmfptback.sys.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.ngkj.zmfptback.sys.util.ValidateCodeUtil;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/7 creation
 **/
@Service
public class ValidateCodeService {

    public Map<String,Object> getVerocde() throws IOException {
        Map<String,Object> map = new HashMap<>();
        ValidateCodeUtil vCode = new ValidateCodeUtil(100,25,4,150);
        String code = vCode.getCode();
        String s = SecureUtil.md5(code);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(vCode.getBuffImg(),"png",stream);
        String base64 = Base64.encode(stream.toByteArray());
        map.put("ImageBase64",base64);
        map.put("codeMd5",s);
        return map;
    }
}
